package com.winit.openapi.wh.builder;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Searchable;
import com.winit.common.query.Sort;
import com.winit.common.query.Sort.Direction;
import com.winit.common.utils.Collections3;
import com.winit.oms.spi.inbound.vo.InboundMerchandiseDetailsVo;
import com.winit.oms.spi.inbound.vo.InboundOrderItemVo;
import com.winit.oms.spi.inbound.vo.InboundPackageVo;
import com.winit.oms.spi.inbound.vo.InboundPackinglistVo;
import com.winit.openapi.constants.ApiConstant;

/**
 * 海外仓入库单构造器 包裹信息
 * 
 * @version <pre>
 * Author	  Version		Date		Changes
 * dinghua.an	1.0  		2015年11月10日 	Created
 *
 * </pre>
 * @since 1.
 */
public class InboundPackageBuilder {

    /**
     * 导出箱单信息 入参分页参数
     * 
     * @param json
     * @return
     */
    public static Searchable buildSearchPageParams(JSONObject json) {
        int pageNo = ApiConstant.PAGE_NO_DEFAULT;
        int pageSize = ApiConstant.EXPORT_MAX_COUNT;
        String sort="";
        String column="";
        String sortColumn="";
        // pageParams
        if (json != null && json.containsKey("pageParams")) {
            JSONObject pageParams = json.getJSONObject("pageParams");
            // pageNo
            if (pageParams.containsKey("pageNo")) {
                pageNo = pageParams.getInteger("pageNo");
            }
            // pageSize
            if (pageParams.containsKey("pageSize")) {
                pageSize = pageParams.getInteger("pageSize");
                if (pageSize > ApiConstant.EXPORT_MAX_COUNT) {
                    pageSize = ApiConstant.EXPORT_MAX_COUNT;
                }
            }
        }
        if(null !=json && json.containsKey("sort")){
            sort =json.getString("sort");
        }
        if(null !=json && json.containsKey("sortColumn")){
            column =json.getString("sortColumn");
        }
        if(!StringUtils.isEmpty(column)){
            if("caseNo".equals(column)){
                sortColumn="p.CASE_NO";
            }
        }else{
        	sortColumn="p.PACKAGE_ID";
        }
        Searchable search = Searchable.newSearchable();
        search.setPage(pageNo, pageSize);
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(sortColumn)){
            if("ASC".equals(sort)){
                search.addSort(new Sort(Direction.ASC, sortColumn));
            }
            if("DESC".equals(sort)){
                search.addSort(new Sort(Direction.DESC, sortColumn));
            }
            
        }else{
            search.addSort(new Sort(Direction.DESC, sortColumn));
        }
      
        
        return search;
    }

    
    /**
     * 商品箱单信息上传 入参封装
     * 
     * @param json
     * @return
     */
    public static List<InboundPackageVo> buildPackage(JSONObject json) {
        List<InboundPackageVo> pList=new ArrayList<InboundPackageVo>();
        if (json.containsKey("packageList")) {
            JSONArray packageList = json.getJSONArray("packageList");
            for (Object object : packageList) {
                JSONObject packageDetails = (JSONObject) object;
                InboundPackageVo  vo = new InboundPackageVo();
                if (packageDetails.containsKey("caseNo")) {
                    vo.setCaseNo(packageDetails.getString("caseNo").trim());
                }
                if (packageDetails.containsKey("sellerWeight")) {
                	if(null != packageDetails.getBigDecimal("sellerWeight") ){
                		vo.setSellerWeight(packageDetails.getBigDecimal("sellerWeight").setScale(6,  BigDecimal.ROUND_HALF_UP));
                	}
                }
                if (packageDetails.containsKey("sellerLength")) {
                	if(null != packageDetails.getBigDecimal("sellerLength") ){
                		vo.setSellerLength(packageDetails.getBigDecimal("sellerLength").setScale(2,  BigDecimal.ROUND_HALF_UP));
                	}
                }
                if (packageDetails.containsKey("sellerWidth")) {
                	if(null != packageDetails.getBigDecimal("sellerWidth") ){
                		vo.setSellerWidth(packageDetails.getBigDecimal("sellerWidth").setScale(2, BigDecimal.ROUND_HALF_UP));
                	}
                }
                if (packageDetails.containsKey("sellerHeight")) {
                	if(null != packageDetails.getBigDecimal("sellerHeight") ){
                		vo.setSellerHeight(packageDetails.getBigDecimal("sellerHeight").setScale(2, BigDecimal.ROUND_HALF_UP));
                	}
                }
                List<InboundMerchandiseDetailsVo> mList= buildMerchandise(packageDetails);
                vo.setMerchandiseDetailsList(mList);
                pList.add(vo);
            }
        }
        return pList;
    }
    
    /**
     * 新增商品箱单信息 入参封装
     * 
     * @param json
     * @return
     */
    public static InboundPackageVo buildAddPackage(JSONObject json) {
        InboundPackageVo vo = new InboundPackageVo();
        if (json.containsKey("orderNo")) {
            vo.setOrderNo(json.getString("orderNo").trim());
        }
        if (json.containsKey("caseNo")) {
            vo.setCaseNo(json.getString("caseNo").trim());
        }
        if (json.containsKey("sellerWeight")) {
        	if(null != json.getBigDecimal("sellerWeight") ){
        		vo.setSellerWeight(json.getBigDecimal("sellerWeight").setScale(6,  BigDecimal.ROUND_HALF_UP));
        	}
        }
        if (json.containsKey("sellerLength")) {
        	if(null != json.getBigDecimal("sellerLength") ){
        		vo.setSellerLength(json.getBigDecimal("sellerLength").setScale(2,  BigDecimal.ROUND_HALF_UP));
        	}
        }
        if (json.containsKey("sellerWidth")) {
        	if(null != json.getBigDecimal("sellerWidth") ){
        		vo.setSellerWidth(json.getBigDecimal("sellerWidth").setScale(2,  BigDecimal.ROUND_HALF_UP));
        	}
        }
        if (json.containsKey("sellerHeight")) {
        	if(null != json.getBigDecimal("sellerHeight") ){
        		vo.setSellerHeight(json.getBigDecimal("sellerHeight").setScale(2,  BigDecimal.ROUND_HALF_UP));
        	}
        }
        List<InboundMerchandiseDetailsVo> mList= buildMerchandise(json);
        vo.setMerchandiseDetailsList(mList);
        return vo;
    }

    /**
     * 编辑箱单信息 入参封装
     * 
     * @param json
     * @return
     */
    public static InboundPackageVo buildEditPackage(JSONObject json) {
        InboundPackageVo vo = new InboundPackageVo();
        if (json.containsKey("orderNo")) {
            vo.setOrderNo(json.getString("orderNo").trim());
        }
        if (json.containsKey("packageId")) {
            vo.setPackageId(json.getLong("packageId"));
        }
        if (json.containsKey("caseNo")) {
            vo.setCaseNo(json.getString("caseNo").trim());
        }
        if (json.containsKey("sellerWeight")) {
            vo.setSellerWeight(json.getBigDecimal("sellerWeight"));
        }
        if (json.containsKey("sellerLength")) {
            vo.setSellerLength(json.getBigDecimal("sellerLength"));
        }
        if (json.containsKey("sellerWidth")) {
            vo.setSellerWidth(json.getBigDecimal("sellerWidth"));
        }
        if (json.containsKey("sellerHeight")) {
            vo.setSellerHeight(json.getBigDecimal("sellerHeight"));
        }
        List<InboundMerchandiseDetailsVo> mList= buildMerchandise(json);
        vo.setMerchandiseDetailsList(mList);
        return vo;
    }
    
    /**
     * 
     * 构建商品信息
     * @param json
     * @param mList
     */
    private static List<InboundMerchandiseDetailsVo> buildMerchandise(JSONObject json){
        List<InboundMerchandiseDetailsVo> mList=new ArrayList<InboundMerchandiseDetailsVo>();
        if (json.containsKey("merchandiseList")) {
            JSONArray merchandiseDetailsList = json.getJSONArray("merchandiseList");
            for (Object o : merchandiseDetailsList) {
                JSONObject merchandiseDetails = (JSONObject) o;
                InboundMerchandiseDetailsVo detailsVo= new InboundMerchandiseDetailsVo();
                if (merchandiseDetails.containsKey("merchandiseCode")) {
                    detailsVo.setMerchandiseCode(merchandiseDetails.getString("merchandiseCode").trim());
                }
                if (merchandiseDetails.containsKey("specifications")) {
                    detailsVo.setSpecification(merchandiseDetails.getString("specifications").trim());
                }
                if (merchandiseDetails.containsKey("quantity")) {
                    detailsVo.setQuantity(merchandiseDetails.getInteger("quantity"));
                }
                if (merchandiseDetails.containsKey("canOrderQuantity")) {
                    detailsVo.setCanOrderQuantity(merchandiseDetails.getInteger("canOrderQuantity"));
                }
                mList.add(detailsVo);
            }
        }
        return mList;
     }

    /**
     * 导出箱单信息 出参封装
     * 
     * @param list
     * @return
     */
    public static List<Map<String, Object>> buildReturnPackage(List<InboundPackageVo> list) {
        List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();
        for (InboundPackageVo vo : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("caseNo", vo.getCaseNo());
            map.put("orderNo", vo.getOrderNo());
            map.put("sellerLength", vo.getSellerLength());
            map.put("sellerWidth", vo.getSellerWidth());
            map.put("sellerHeight", vo.getSellerHeight());
            map.put("sellerWeight", vo.getSellerWeight());
            List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
            List<InboundMerchandiseDetailsVo> mdList = vo.getMerchandiseDetailsList();
            for (InboundMerchandiseDetailsVo v : mdList) {
                Map<String, Object> m = new HashMap<String, Object>();
                m.put("merchandiseCode", v.getMerchandiseCode());
                m.put("quantity", v.getQuantity());
                m.put("specifications", v.getSpecification());
                mapList.add(m);
            }
            map.put("merchandiseList", mapList);
            mList.add(map);
        }
        return mList;
    }

    /**
     * 查询包裹信息 出参封装
     * 
     * @param list
     * @return
     */
    public static List<Map<String, Object>> buildReturnPackageList(List<InboundPackageVo> list) {
        List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();
        for (InboundPackageVo vo : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("caseNo", vo.getCaseNo());
            map.put("packageId", vo.getPackageId());
            map.put("sellerLength", vo.getSellerLength());
            map.put("sellerWidth", vo.getSellerWidth());
            map.put("sellerHeight", vo.getSellerHeight());
            map.put("sellerWeight", vo.getSellerWeight());
            map.put("length", vo.getLength());
            map.put("width", vo.getWidth());
            map.put("height", vo.getHeight());
            map.put("weight", vo.getWeight());
            map.put("createDate", vo.getCreated());
            map.put("packageNo", vo.getPackageSerNo());
            map.put("packageLevel", vo.getPackageLevel());
            map.put("status", vo.getStatus());
            map.put("skuQty", vo.getSkuQty());
            map.put("itemQty", vo.getSkuQty());
            if(Collections3.isNotEmpty(vo.getMerchandiseDetailsList())){
                map.put("merchandiseCode", vo.getMerchandiseDetailsList().get(0).getMerchandiseCode()); 
                map.put("merchandiseSerno", vo.getMerchandiseDetailsList().get(0).getMerchandiseSerno());
                map.put("nameCn", vo.getMerchandiseDetailsList().get(0).getNameCn());
                map.put("quantity", vo.getMerchandiseDetailsList().get(0).getQuantity());
            }
            mList.add(map);
        }
        return mList;
    }
    
    /**
     * 查询包裹信息 出参封装
     * 
     * @param list
     * @return
     */
    public static List<Map<String, Object>> buildReturnPackingList(List<InboundPackinglistVo> list) {
        List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();
        for (InboundPackinglistVo vo : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("caseNo", vo.getCaseNo());
            map.put("packageSerno", vo.getPackageSerNo());
            map.put("sku", vo.getSku());
            map.put("specification", vo.getSpecification());
            map.put("merchandiseCode", vo.getMerchandiseCode());
            map.put("quantity", vo.getQuantity());
            mList.add(map);
        }
        return mList;
    }

    /**
     * 查询商品信息 出参封装
     * 
     * @param list
     * @return
     */
    public static Map<String, Object> buildReturnMerchandiseList(List<InboundMerchandiseDetailsVo> mlist,List<InboundOrderItemVo> itemList) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        for (InboundMerchandiseDetailsVo v : mlist) {
            Map<String, Object> m = new HashMap<String, Object>();
            m.put("merchandiseCode", v.getMerchandiseCode());
            m.put("merchandiseId", v.getInboundMerchandiseId());
            m.put("quantity", v.getQuantity());
            m.put("actualQuantity", v.getActualQty());
            m.put("merchandiseSerNo", v.getMerchandiseSerno());
            m.put("merchandiseNameCn", v.getNameCn());
            m.put("merchandiseNameEn", v.getNameEn());
            m.put("specification", v.getSpecification());
            m.put("isSingleManagement", v.getIsSingleManagement());
            m.put("estimateLength",v.getSellerLength() );
            m.put("estimateWidth", v.getSellerWidth());
            m.put("estimateHeight", v.getSellerHeight());
            m.put("estimateWeight", v.getSellerWeight());
            m.put("length", v.getLength());
            m.put("width", v.getWidth());
            m.put("height", v.getHeight());
            m.put("weight", v.getWeight());
            m.put("shelveQty", v.getShelveQty());
            List<String> currentItemList = new ArrayList<String>();
            if(!CollectionUtils.isEmpty(itemList)){
            	for (InboundOrderItemVo itemVo : itemList) {
					if(v.getMerchandiseSerno().equals(itemVo.getMerchandiseSerno())){
						currentItemList.add(itemVo.getItemSerno());
					}
				}
            }
            m.put("isItemExists", CollectionUtils.isEmpty(currentItemList)?"N":"Y");
            m.put("itemSerno", currentItemList);
            mapList.add(m);
        }
        map.put("merchandiseList", mapList);
        return map;
    }

}
